<template>
  <div class="global-buttons">
    <el-button
      type="primary"
      circle
      class="global-button storage-btn"
      @click="openStorageDialog"
      title="入库"
    >
      <i class="el-icon-download"></i>
    </el-button>
    
    <el-button
      type="success"
      circle
      class="global-button checkout-btn"
      @click="openCheckoutDialog"
      title="结算"
    >
      <i class="el-icon-wallet"></i>
    </el-button>
    
    <!-- 入库弹窗 -->
    <el-dialog
      :visible.sync="storageDialogVisible"
      width="90%"
      :close-on-click-modal="false"
      :before-close="handleCloseDialog"
      :fullscreen="true"
      :modal-append-to-body="false"
      :append-to-body="true"
      :show-close="false"
      custom-class="custom-dialog"
    >
      <storage-view 
        @close="handleCloseDialog('storage')" 
        @refresh-products="refreshProducts"
        ref="storageView" 
      />
    </el-dialog>
    
    <!-- 结算弹窗 -->
    <el-dialog
      :visible.sync="checkoutDialogVisible"
      width="90%"
      :close-on-click-modal="false"
      :before-close="handleCloseDialog"
      :fullscreen="true"
      :modal-append-to-body="false"
      :append-to-body="true"
      :show-close="false"
      custom-class="custom-dialog"
    >
      <checkout-view 
        @close="handleCloseDialog('checkout')" 
        @refresh-products="refreshProducts"
        ref="checkoutView" 
      />
    </el-dialog>
  </div>
</template>

<script>
import StorageView from './StorageView.vue'
import CheckoutView from './CheckoutView.vue'

export default {
  name: 'GlobalButtons',
  components: {
    StorageView,
    CheckoutView
  },
  data() {
    return {
      storageDialogVisible: false,
      checkoutDialogVisible: false
    }
  },
  methods: {
    openStorageDialog() {
      this.storageDialogVisible = true
      // 等待组件渲染完成后加载商品列表
      this.$nextTick(() => {
        if (this.$refs.storageView) {
          this.$refs.storageView.loadProducts()
        }
      })
    },
    openCheckoutDialog() {
      this.checkoutDialogVisible = true
      // 等待组件渲染完成后加载商品列表
      this.$nextTick(() => {
        if (this.$refs.checkoutView) {
          this.$refs.checkoutView.loadProducts()
        }
      })
    },
    handleCloseDialog(type) {
      if (type === 'storage') {
        this.storageDialogVisible = false
      } else if (type === 'checkout') {
        this.checkoutDialogVisible = false
      } else {
        this.storageDialogVisible = false
        this.checkoutDialogVisible = false
      }
    },
    refreshProducts() {
      console.log('接收到刷新商品列表事件');
      // 触发全局事件，通知商品列表组件刷新
      this.$bus.$emit('refresh-product-list');
    }
  }
}
</script>

<style scoped>
.global-buttons {
  position: fixed;
  right: 40px;
  bottom: 40px;
  display: flex;
  flex-direction: row;
  gap: 20px;
  z-index: 2000;
}

.global-button {
  width: 60px;
  height: 60px;
  font-size: 24px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.global-button:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 20px 0 rgba(0, 0, 0, 0.15);
}

.storage-btn {
  background-color: #304156;
  border-color: #304156;
}

.checkout-btn {
  background-color: #ff6b00;
  border-color: #ff6b00;
}

.el-dialog__body {
  padding: 0;
  height: calc(100vh - 120px);
  overflow: hidden;
}

.custom-dialog {
  display: flex;
  flex-direction: column;
  max-height: 100vh;
  margin: 0 !important;
}

.custom-dialog .el-dialog__header {
  display: none;
}

.custom-dialog .el-dialog__body {
  padding: 0;
  margin: 0;
  height: 100vh;
  overflow: hidden;
}

.custom-dialog .el-dialog__headerbtn .el-dialog__close {
  color: #fff;
}

/* 覆盖Element UI的默认样式 */
.el-dialog.custom-dialog {
  margin: 0 !important;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 0;
  border-radius: 0;
  background-color: transparent;
  display: flex;
  flex-direction: column;
}

.custom-dialog .el-dialog__body {
  padding: 0 !important;
  margin: 0 !important;
  height: 100% !important;
  overflow: hidden;
  flex: 1;
}

/* 额外的重置样式，确保没有顶部留白 */
body.el-popup-parent--hidden {
  padding: 0 !important;
  margin: 0 !important;
}
</style> 